package com.myeasyexcel.utils;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.fastjson.JSON;
import com.myeasyexcel.constant.CustomException;
import com.myeasyexcel.response.ExcelCode;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author wangXiaoHu
 * @date 2022/06/07 10:42
 **/
public interface IEasyExcelUtil {
    /**
     * 导入excel并导出错误数据
     *
     * @param response      响应体
     * @param file          上传的excel文件
     * @param clazz         导出的实体类class对象
     * @param headRowNumber 标题所在行号（从1开始）
     */
    default void importExcel(HttpServletResponse response, EasyExcelListener<? extends ExcelErrMsg> listener
            , MultipartFile file, Class<? extends ExcelErrMsg> clazz, String fileName, Integer headRowNumber){
        EasyExcelUtil.verifyImportExcelFile(file, clazz);
        // 以下是 处理逻辑
        try {
            listener.setTClass(clazz);
            EasyExcel.read(file.getInputStream(), clazz, listener).sheet()
                    .headRowNumber(headRowNumber).doRead();
            long time2 = System.currentTimeMillis();
            String s = JSON.toJSONString(listener.getErrList());
            List<? extends ExcelErrMsg> errList = JSON.parseArray(s, clazz);

            // 设置导出文件类型
            String originalFilename = file.getOriginalFilename();
            ExcelTypeEnum excelTypeEnum = null;
            assert originalFilename != null;
            if (originalFilename.endsWith(ExcelTypeEnum.XLSX.getValue())) {
                excelTypeEnum = ExcelTypeEnum.XLSX;
            } else if (originalFilename.endsWith(ExcelTypeEnum.XLS.getValue())) {
                excelTypeEnum = ExcelTypeEnum.XLS;
            }
            this.exportErrorExcel(response, errList, listener.getSuccessSize(), clazz, fileName, excelTypeEnum);
        } catch (IOException e) {
            e.printStackTrace();
            throw new CustomException(ExcelCode.TEMPLATE_IO_ERROR);
        } catch (NullPointerException ex) {
            ex.printStackTrace();
        }
    };

    /**
     * 导出excel
     *
     * @param response 响应体
     * @param list     要导出的数据
     * @param clazz    导出的实体类class对象
     * @param fileName 导出的文件名称
     */
    default <T> void exportExcel(HttpServletResponse response, List<T> list, Class<T> clazz, String fileName){
        EasyExcelUtil.exportExcel(response,list,clazz,fileName);
    };

    /**
     * 导出错误的excel文件
     *
     * @param response    响应体
     * @param errorList   错误的list集合
     * @param successSize 成功的数量
     * @param clazz       导出的实体class对象
     * @param fileName    导出的文件名称
     */
    default void exportErrorExcel(HttpServletResponse response, List<? extends ExcelErrMsg> errorList
            , int successSize, Class<? extends ExcelErrMsg> clazz, String fileName, ExcelTypeEnum excelTypeEnum){
    };


    default void importExcelPlus(HttpServletResponse response, EasyExcelListenerPlus<? extends ExcelErrMsg> listener
            , MultipartFile file, Class<? extends ExcelErrMsg> clazz, String fileName, Integer headRowNumber){
        EasyExcelUtil.verifyImportExcelFile(file, clazz);
        // 以下是 处理逻辑
        try {
            long time1 = System.currentTimeMillis();
            listener.setTClass(clazz);
            EasyExcel.read(file.getInputStream(), clazz, listener).sheet()
                    .headRowNumber(headRowNumber).doRead();
            long time2 = System.currentTimeMillis();
            String s = JSON.toJSONString(listener.getErrList());
            List<? extends ExcelErrMsg> errList = JSON.parseArray(s, clazz);

            // 设置导出文件类型
            String originalFilename = file.getOriginalFilename();
            ExcelTypeEnum excelTypeEnum = null;
            assert originalFilename != null;
            if (originalFilename.endsWith(ExcelTypeEnum.XLSX.getValue())) {
                excelTypeEnum = ExcelTypeEnum.XLSX;
            } else if (originalFilename.endsWith(ExcelTypeEnum.XLS.getValue())) {
                excelTypeEnum = ExcelTypeEnum.XLS;
            }
            exportErrorExcel(response, errList, listener.getSuccessSize(), clazz, fileName, excelTypeEnum);
        } catch (IOException e) {
            e.printStackTrace();
            throw new CustomException(ExcelCode.TEMPLATE_IO_ERROR);
        } catch (NullPointerException ex) {
            ex.printStackTrace();
        }
    };
}
